﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RockCompany.Common.Constant;
using System.Data;
using System.Windows.Forms;
using System.Drawing;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace RockCompany.Common.Lib
{
    public class Util
    {
        public static bool parseBoolean(object bValue)
        {
            bool b = false;
            if (bValue != null)
            {
                try
                {
                    b = bool.Parse(bValue.ToString());
                }
                catch (Exception)
                {
                    b = false;
                }
            }
            return b;
        }

        public static decimal parseDecimal(object dValue)
        {
            decimal number = decimal.Zero;
            if (dValue != null)
            {
                try
                {
                    number = decimal.Parse(dValue.ToString());
                }
                catch (Exception)
                {
                    number = decimal.Zero;
                }
            }
            return number;
        }

        public static double parseDouble(string str)
        {
            double number = 0.0;
            try
            {
                number = Double.Parse(str);
            }
            catch (Exception)
            {
                number = double.NaN;
            }
            return number;
        }

        public static int parseInteger(object ob)
        {
            int number = 0;
            try
            {
                number = int.Parse(Util.parseString(ob));
            }
            catch (Exception)
            {
            }
            return number;
        }

        public static DateTime parseDateTime(object obj)
        {
            DateTime date = DateTime.Today.Date;
            try
            {
                if (obj != null)
                {
                    date = DateTime.Parse(obj.ToString());
                }
            }
            catch (Exception)
            {
                //date = null;
            }
            return date;
        }

        public static string parseString(object obj)
        {
            if (obj == null)
            {
                return CommonConstantIF.BLANK;
            }
            return obj.ToString();
        }

        // Get a list of column name of a DataTable, except column CHON
        public static List<string> ColumnNames(DataTable dt)
        {
            List<string> lst = new List<string>();
            // For each DataTable, store the ColumnName.
            foreach (DataColumn col in dt.Columns)
            {
                if (!"CHON".Equals(col.ColumnName))
                {
                    lst.Add(col.ColumnName);
                }
            }

            return lst;
        }

        // Mark selected on column CHON that its type is bool
        public static void MarkSelected(DataTable dt)
        {
            if (dt == null || !dt.Columns.Contains(CommonConstantIF.CHON)) { return; }
            foreach (DataRow row in dt.Rows)
            {
                if (row.RowState == DataRowState.Added)
                {
                    row[CommonConstantIF.CHON] = true;
                }
            }
        }

        public static List<DataRow> GetSelectedRows(DataTable dataTable)
        {
            List<DataRow> lst = new List<DataRow>();
            foreach (DataRow row in dataTable.Rows)
            {
                if (row[CommonConstantIF.CHON].Equals(true))
                {
                    lst.Add(row);
                }
            }
            return lst;
        }

        public static string FormatDate(DateTime date)
        {
            return string.Format("{0:" + CommonConstantIF.DATE_FORMAT + "}", date);
        }

        public static string FormatDecimal(object dec)
        {
            return string.Format("{0:#,###.##}", parseDecimal(dec));
        }

        // Set rowstate modified to all rows
        public static void SetRowStatusMod(DataTable dt)
        {
            if (dt != null || dt.Rows.Count > 0)
            {
                dt.AcceptChanges();
                foreach (DataRow modRow in dt.Rows)
                {
                    modRow.SetModified();
                }
            }
        }

        public static bool saveSerialized(object ob, string sPath)
        {
            bool isSaveSuccess = false;
            try
            {
                Stream stream = new FileStream(sPath, FileMode.Create, FileAccess.Write);
                BinaryFormatter bnFormatter = new BinaryFormatter();
                bnFormatter.Serialize(stream, ob);
                stream.Close();
                isSaveSuccess = true;
            }
            catch (Exception ex)
            {
            }
            return isSaveSuccess;
        }

        public static object readSerialized(string sPath)
        {

            Stream stream = new FileStream(sPath, FileMode.Open, FileAccess.Read);
            BinaryFormatter bnFormatter = new BinaryFormatter();
            object ob =  bnFormatter.Deserialize(stream);
            stream.Close();
            return ob;
        }
    }
}
